using Decal.Adapter;
using Decal.Adapter.Wrappers;
using System;

namespace SixthSense
{
    public partial class PluginCore : PluginBase
    {
        private void initWorldFilter()
        {
            //////////////////////////////////////////////////////////////
            // To initialize any of the World Filter Events,            //
            // simply uncomment the appropriate initialization          //
            // statement(s) below to enable the event handler           //
            //////////////////////////////////////////////////////////////

            // Initialize the ResetTrade event handler
            //Core.WorldFilter.ResetTrade += new EventHandler<Decal.Adapter.Wrappers.ResetTradeEventArgs>(WorldFilter_ResetTrade);

            // Initialize the ReleaseObject event handler
            Core.WorldFilter.ReleaseObject += new EventHandler<Decal.Adapter.Wrappers.ReleaseObjectEventArgs>(WorldFilter_ReleaseObject);

            // Initialize the ReleaseDone event handler
            //Core.WorldFilter.ReleaseDone += new EventHandler(WorldFilter_ReleaseDone);

            // Initialize the MoveObject event handler
            Core.WorldFilter.MoveObject += new EventHandler<Decal.Adapter.Wrappers.MoveObjectEventArgs>(WorldFilter_MoveObject);

            // Initialize the FailToCompleteTrade event handler
            //Core.WorldFilter.FailToCompleteTrade += new EventHandler(WorldFilter_FailToCompleteTrade);

            // Initialize the FailToAddTradeItem event handler
            //Core.WorldFilter.FailToAddTradeItem += new EventHandler<Decal.Adapter.Wrappers.FailToAddTradeItemEventArgs>(WorldFilter_FailToAddTradeItem);

            // Initialize the EnterTrade event handler
            //Core.WorldFilter.EnterTrade += new EventHandler<Decal.Adapter.Wrappers.EnterTradeEventArgs>(WorldFilter_EnterTrade);

            // Initialize the EndTrade event handler
            //Core.WorldFilter.EndTrade += new EventHandler<Decal.Adapter.Wrappers.EndTradeEventArgs>(WorldFilter_EndTrade);

            // Initialize the DeclineTrade event handler
            //Core.WorldFilter.DeclineTrade += new EventHandler<Decal.Adapter.Wrappers.DeclineTradeEventArgs>(WorldFilter_DeclineTrade);

            // Initialize the CreateObject event handler
            Core.WorldFilter.CreateObject += new EventHandler<Decal.Adapter.Wrappers.CreateObjectEventArgs>(WorldFilter_CreateObject);

            // Initialize the ChangeObject event handler
            Core.WorldFilter.ChangeObject += new EventHandler<Decal.Adapter.Wrappers.ChangeObjectEventArgs>(WorldFilter_ChangeObject);

            // Initialize the ApproachVendor event handler
            //Core.WorldFilter.ApproachVendor += new EventHandler<Decal.Adapter.Wrappers.ApproachVendorEventArgs>(WorldFilter_ApproachVendor);

            // Initialize the AddTradeItem event handler
            //Core.WorldFilter.AddTradeItem += new EventHandler<Decal.Adapter.Wrappers.AddTradeItemEventArgs>(WorldFilter_AddTradeItem);

            // Initialize the AcceptTrade event handler
            //Core.WorldFilter.AcceptTrade += new EventHandler<Decal.Adapter.Wrappers.AcceptTradeEventArgs>(WorldFilter_AcceptTrade);
        }

        void WorldFilter_AcceptTrade(object sender, Decal.Adapter.Wrappers.AcceptTradeEventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_AddTradeItem(object sender, Decal.Adapter.Wrappers.AddTradeItemEventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_ApproachVendor(object sender, Decal.Adapter.Wrappers.ApproachVendorEventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_ChangeObject(object sender, Decal.Adapter.Wrappers.ChangeObjectEventArgs e)
        {
            // this only covers my main pack for now.. need to follow original logic
            if (e.Changed.Container == PlayerObject.ID)
            {
                return;
            }

            if (e.Change == WorldChangeType.IdentReceived)
            {
                WriteToChat("WorldFilter_ChangeObject: " + e.Changed.Name + " " + e.Change);

                MaterialInfo material = _Config.CurrentProfile.Materials.FindById(e.Changed.Values(LongValueKey.Material));

                if ((material != null) && (material.Workmanship > 0))
                {
                    WriteToChat("Create Object material check: " + e.Changed.Values(LongValueKey.Material) + " = " + e.Changed.Values(LongValueKey.Workmanship) + " (target: " + material.Workmanship + ")");
                    if (material.Workmanship >= e.Changed.Values(LongValueKey.Workmanship))
                    {
                        //AddItem(e.Changed, "item");
                    }
                }                
            }

        }

        void WorldFilter_CreateObject(object sender, Decal.Adapter.Wrappers.CreateObjectEventArgs e)
        {
            // TODO track what is my inventory

            CoordsObject coords = e.New.Coordinates();
            string DetectReason = "";

            // this only covers my main pack for now.. need to follow original logic
            if (e.New.Container == PlayerObject.ID )
            {
                    return;
            }

            int spell = e.New.Values(LongValueKey.AssociatedSpell);

            bool DoAlert = false;
            string ItemType = "special";

            if ((_Config.CurrentOptions.TrackPortals) && (e.New.ObjectClass == ObjectClass.Portal ))
            {
                if (e.New.Name.StartsWith("Destroyed") == false)
                {
                    DoAlert = true;
                    DetectReason = "Portal";
                    ItemType = "item";
                }
            }

            // Compare for value
            if(e.New.Values(LongValueKey.Value) >= _Config.CurrentOptions.MinValue)
            {
                DoAlert = true;
                DetectReason = "Value";
            }

            AlertInfo item = _Config.CurrentProfile.Alerts.FindByName(e.New.Name);
            if (item != null)
            {
                DoAlert = true;
                DetectReason = "Name";
                ItemType = item.Type;
            }

            // this is going to cause some problems, but I dont see how to get landblock info to follow original logic yet
            // I have seen some portals show up over 1 unit away.  This will mean they won't show on the tracker.  Not ideal.
            // may mean I have to start tracking objects based on messages again
            double distance = Core.WorldFilter.Distance(PlayerObject.ID, e.New.Id,true);
            if ((DoAlert) && (distance < _RemoveDistance))
            {
                if (_Config.CurrentOptions.Popup)
                {
                    // TODO figure out how to pop up
                }

                //AddItem(e.New, ItemType);
            }
        }

        void WorldFilter_DeclineTrade(object sender, Decal.Adapter.Wrappers.DeclineTradeEventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_EndTrade(object sender, Decal.Adapter.Wrappers.EndTradeEventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_EnterTrade(object sender, Decal.Adapter.Wrappers.EnterTradeEventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_FailToAddTradeItem(object sender, Decal.Adapter.Wrappers.FailToAddTradeItemEventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_FailToCompleteTrade(object sender, EventArgs e)
        {
            // DO STUFF HERE
        }

        void WorldFilter_MoveObject(object sender, Decal.Adapter.Wrappers.MoveObjectEventArgs e)
        {
            // if it's me, update the main page coords
            if (e.Moved.Id == Core.CharacterFilter.Id)
            {
                CoordsObject coords = e.Moved.Coordinates();
                PlayerObject.EWCoordinate = coords.EastWest;
                PlayerObject.NSCoordinate = coords.NorthSouth;

                CoordsText.Text = coords.ToString();

                // before we spend cycles on updating coordinates, lets see if we can delete anything
                RemoveDistantObjects();
                
                // let the tracker figure out of it needs to update coords
                // Only do this as part of the player update since things shouldnt move
                if (_Config.CurrentOptions.RelativeCoords)
                {
                    //UpdateItemLocations();
                }
            }
            // TODO deal with things that could move.  Really? do I want to track things that move? mobs moving, or things wielded? Not really

            // TODO track moving in and out of my inventory, etc.
        }

        void WorldFilter_ReleaseDone(object sender, EventArgs e)
        {
        }

        void WorldFilter_ReleaseObject(object sender, Decal.Adapter.Wrappers.ReleaseObjectEventArgs e)
        {
        }

        void WorldFilter_ResetTrade(object sender, Decal.Adapter.Wrappers.ResetTradeEventArgs e)
        {
            // DO STUFF HERE
        }

        private void destroyWorldFilter()
        {
            // UnInitialize the ResetTrade event handler
            // Core.WorldFilter.ResetTrade -= new EventHandler<Decal.Adapter.Wrappers.ResetTradeEventArgs>(WorldFilter_ResetTrade);

            // UnInitialize the ReleaseObject event handler
            Core.WorldFilter.ReleaseObject -= new EventHandler<Decal.Adapter.Wrappers.ReleaseObjectEventArgs>(WorldFilter_ReleaseObject);

            // UnInitialize the ReleaseDone event handler
            // Core.WorldFilter.ReleaseDone -= new EventHandler(WorldFilter_ReleaseDone);

            // UnInitialize the MoveObject event handler
            Core.WorldFilter.MoveObject -= new EventHandler<Decal.Adapter.Wrappers.MoveObjectEventArgs>(WorldFilter_MoveObject);

            // UnInitialize the FailToCompleteTrade event handler
            // Core.WorldFilter.FailToCompleteTrade -= new EventHandler(WorldFilter_FailToCompleteTrade);

            // UnInitialize the FailToAddTradeItem event handler
            // Core.WorldFilter.FailToAddTradeItem -= new EventHandler<Decal.Adapter.Wrappers.FailToAddTradeItemEventArgs>(WorldFilter_FailToAddTradeItem);

            // UnInitialize the EnterTrade event handler
            // Core.WorldFilter.EnterTrade -= new EventHandler<Decal.Adapter.Wrappers.EnterTradeEventArgs>(WorldFilter_EnterTrade);

            // UnInitialize the EndTrade event handler
            // Core.WorldFilter.EndTrade -= new EventHandler<Decal.Adapter.Wrappers.EndTradeEventArgs>(WorldFilter_EndTrade);

            // UnInitialize the DeclineTrade event handler
            // Core.WorldFilter.DeclineTrade -= new EventHandler<Decal.Adapter.Wrappers.DeclineTradeEventArgs>(WorldFilter_DeclineTrade);

            // UnInitialize the CreateObject event handler
            Core.WorldFilter.CreateObject -= new EventHandler<Decal.Adapter.Wrappers.CreateObjectEventArgs>(WorldFilter_CreateObject);

            // UnInitialize the ChangeObject event handler
            Core.WorldFilter.ChangeObject -= new EventHandler<Decal.Adapter.Wrappers.ChangeObjectEventArgs>(WorldFilter_ChangeObject);

            // UnInitialize the ApproachVendor event handler
            // Core.WorldFilter.ApproachVendor -= new EventHandler<Decal.Adapter.Wrappers.ApproachVendorEventArgs>(WorldFilter_ApproachVendor);

            // UnInitialize the AddTradeItem event handler
            // Core.WorldFilter.AddTradeItem -= new EventHandler<Decal.Adapter.Wrappers.AddTradeItemEventArgs>(WorldFilter_AddTradeItem);

            // UnInitialize the AcceptTrade event handler
            // Core.WorldFilter.AcceptTrade -= new EventHandler<Decal.Adapter.Wrappers.AcceptTradeEventArgs>(WorldFilter_AcceptTrade);
        }
    }
}